package com.myapp.ruyixing.ui;

import static android.view.View.GONE;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.ui.widget.TitleBar;

public class RegisterActivity extends FragmentActivity implements
		OnClickListener, ViewPager.OnPageChangeListener, IShowLoading {

	ViewPager mViewPager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_register);

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		findViewById(R.id.text_phone_regist).setOnClickListener(this);
		findViewById(R.id.text_email_regist).setOnClickListener(this);
		findViewById(R.id.text_phone_regist).setSelected(true);
		findViewById(R.id.text_email_regist).setSelected(false);

		mViewPager = (ViewPager) findViewById(R.id.view_pager);
		MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
		mViewPager.setAdapter(adapter);
		mViewPager.setOnPageChangeListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.text_phone_regist:
			findViewById(R.id.text_phone_regist).setSelected(true);
			findViewById(R.id.text_email_regist).setSelected(false);
			mViewPager.setCurrentItem(0);
			break;
		case R.id.text_email_regist:
			findViewById(R.id.text_phone_regist).setSelected(false);
			findViewById(R.id.text_email_regist).setSelected(true);
			mViewPager.setCurrentItem(1);
			break;

		default:
			break;
		}
	}

	private class MyAdapter extends FragmentPagerAdapter {

		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			switch (arg0) {
			case 0:
				return new PhoneRegisterFragment();
			case 1:
				return new EmailRegisterFragment();

			default:
				break;
			}
			return null;
		}

		@Override
		public int getCount() {
			return 2;
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		if (arg0 == 0) {
			findViewById(R.id.text_phone_regist).setSelected(true);
			findViewById(R.id.text_email_regist).setSelected(false);
		} else {
			findViewById(R.id.text_phone_regist).setSelected(false);
			findViewById(R.id.text_email_regist).setSelected(true);
		}
	}

	@Override
	public void showLoading(boolean show, String msg) {
		if (isFinishing()) {
			return;
		}
		if (show) {
			findViewById(R.id.layout_loading).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.text_loading)).setText(msg);
		} else {
			findViewById(R.id.layout_loading).setVisibility(GONE);
		}
	}
}
